(* ========================================================================
                  Universal Stream Package Format Utilites

     USF Source Texture Image for GLScene
   ------------------------------------------------------------------------
     File: GLUSFSourceImage.pas
     Copyright: (c) 2010, TSX Software
     License: Freeware
   ======================================================================== *)
unit GLUSFSourceImage;

interface

uses Classes, GLTexture, uPackage, OpenGL1x, GLGraphics, GLCrossPlatform;

type
  TGLUSFSourceImage = class(TGLPictureImage)
  private
    FLink: TUSFTextureEntry;
    procedure SetLink(const Value: TUSFTextureEntry);
  protected
    function GetWidth: Integer; override;
    function GetHeight: Integer; override;
    function GetDepth: Integer; override;
  public
    procedure Assign(Source: TPersistent); override;

    // Only picture file name is saved
    procedure SaveToFile(const fileName: string); override;
    // Load picture file name or use fileName as picture filename.
    procedure LoadFromFile(const fileName: string); override;

    class function FriendlyName: string; override;
    class function FriendlyDescription: string; override;
    property NativeTextureTarget;

    function GetBitmap32(target: TGLUInt = GL_TEXTURE_2D): TGLBitmap32; override;
    procedure Invalidate; override;
  published
    // Entry to use.
    property UseTextureEntry: TUSFTextureEntry read FLink write SetLink;
  end;

implementation

uses uPackageUtils;

{ TGLUSFSourceImage }

procedure TGLUSFSourceImage.Assign(Source: TPersistent);
begin
  if (Source is TGLUSFSourceImage) then
    Self.FLink := TGLUSFSourceImage(Source).UseTextureEntry;
end;

class function TGLUSFSourceImage.FriendlyDescription: string;
begin
  Result := 'Image class for loading graphics from USF package';
end;

class function TGLUSFSourceImage.FriendlyName: string;
begin
  Result := 'USF Source Image';
end;

function TGLUSFSourceImage.GetBitmap32(target: TGLUInt): TGLBitmap32;
var gr: TGLGraphic;
begin
  if not Assigned(FLink) then begin
    Result := inherited GetBitmap32(target);
    exit;
  end;

  Picture.OnChange := nil;
  try
    gr := USFTextureToGLGraphic(FLink);
    if Assigned(gr) then begin
      Picture.Graphic := gr;
      gr.Free;
    end;
    Result := inherited GetBitmap32(target);
    Picture.Graphic := nil;
  finally
    Picture.OnChange := PictureChanged;
  end;
end;

function TGLUSFSourceImage.GetDepth: Integer;
begin
  Result := 0;
end;

function TGLUSFSourceImage.GetHeight: Integer;
begin
  if Assigned(FLink) then
    Result := FLink.Height
  else Result := 0;
end;

function TGLUSFSourceImage.GetWidth: Integer;
begin
  if Assigned(FLink) then
    Result := FLink.Width
  else Result := 0;
end;

procedure TGLUSFSourceImage.Invalidate;
begin
  Picture.OnChange := nil;
  try
    Picture.Assign(nil);
  finally
    Picture.OnChange := PictureChanged;
  end;
  inherited;
end;

procedure TGLUSFSourceImage.LoadFromFile(const fileName: string);
begin
  inherited;
end;

procedure TGLUSFSourceImage.SaveToFile(const fileName: string);
begin
  inherited;
end;

procedure TGLUSFSourceImage.SetLink(const Value: TUSFTextureEntry);
begin
  FLink := Value;
  Invalidate;
end;

initialization
  RegisterGLTextureImageClass(TGLUSFSourceImage);

end.